package com.wangyadong.hobby.schedule.jobtask.dsa;


public class RemoveOuterParentheses {

    public String solution(String s) {
        char[] chars = s.toCharArray();
        int right = 0;
        int left = 0;
        StringBuilder stringBuilder = new StringBuilder();
        int startIndex = 0;
        int endIndex = 0;
        for (char aChar : chars) {
            if (aChar == '(') {
                right += 1;
            } else {
                left += 1;
            }
            if (left == right) {
                for (int i = startIndex + 1; i < endIndex; i++) {
                    stringBuilder.append(chars[i]);
                }
                startIndex = endIndex + 1;
            }
            endIndex++;
        }

        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        //(()())(())
        RemoveOuterParentheses parentheses = new RemoveOuterParentheses();
        String solution = parentheses.solution("(()())(())");
        System.out.println(solution);

    }

    public static void main1(String[] args) throws RuntimeException {
        
    }
}
